package com.hb.trade.api.routine.controller.management;

import com.hb.core.pojo.center.au.dto.RoleDTO;
import com.hb.core.pojo.center.sys.dto.ConsoleItemDTO;
import com.hb.core.pojo.center.sys.entity.ConsoleItem;
import com.hb.core.pojo.center.sys.entity.ConsoleItemStatus;
import com.hb.core.pojo.enums.RoleDataAuthEnum;
import com.hb.core.pojo.enums.RoleSignEnum;
import com.hb.core.pojo.enums.RoleTypeEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.client.center.ConsoleItemClient;
import com.hb.trade.client.center.RoleClient;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.LayuiResponseDto;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Version(1.0)
@Api(tags = {"后台管理接口, 非业务接口"})
@RequestMapping("/{version}")
@RestController
public class ApiManagementController {

    protected final ConsoleItemClient consoleItemClient;
    protected final RoleClient roleClient;

    public ApiManagementController(ConsoleItemClient consoleItemClient,
                                   RoleClient roleClient) {
        this.consoleItemClient = consoleItemClient;
        this.roleClient = roleClient;
    }

    // * =========== 后台维护功能接口, 不参与交易平台的接口调用 start

    @PostMapping("/management/consoleItem/itemList")
    public ResponseDto<?> itemList() {
        Message<List<ConsoleItem>> message = consoleItemClient.itemList();
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/add")
    public ResponseDto<?> add(@RequestBody ConsoleItem item) {
        Message<Boolean> message = consoleItemClient.add(item);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/update")
    public ResponseDto<?> update(@RequestBody ConsoleItem item) {
        Message<Boolean> message = consoleItemClient.update(item);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/delete")
    public ResponseDto<?> delete(@RequestBody ConsoleItem item) {
        Message<Boolean> message = consoleItemClient.delete(item);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/transferTo")
    public ResponseDto<?> transferTo(@RequestBody ConsoleItemDTO item) {
        Message<Boolean> message = consoleItemClient.transferTo(item);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/itemChecked")
    public ResponseDto<?> itemChecked(@RequestBody ConsoleItemStatus itemStatus) {
        Message<List<ConsoleItemDTO>> message = consoleItemClient.itemChecked(itemStatus);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/getRoleSignsAndBidWay")
    public ResponseDto<?> getRoleSignsAndBidWay(Long consoleItemId) {
        Message<ConsoleItem> message = consoleItemClient.getRoleSignsAndBidWay(consoleItemId);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/updateRelation")
    public ResponseDto<?> updateRelation(@RequestBody ConsoleItemDTO item) {
        Message<Boolean> message = consoleItemClient.updateRelation(item);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/consoleItem/updateItemRolesAndBidWays")
    public ResponseDto<?> updateItemRolesAndBidWays(Long itemId, String roleTypes, String bidWays) {
        Message<Boolean> message = consoleItemClient.updateItemRolesAndBidWays(itemId, roleTypes, bidWays);
        return ResultUtil.msg(message);
    }

    @PostMapping("/management/role/allRoles")
    public LayuiResponseDto allRoles(@RequestBody RoleDTO roleDTO) {
        Message<PageResult<RoleDTO>> message = roleClient.pageManagementRoles(roleDTO);
        if (message.isSuccess()) {
            PageResult<RoleDTO> data = message.getData();
            List<RoleDTO> records = data.getRecords();
            for (RoleDTO record : records) {
                RoleSignEnum signEnum = Enums.of(RoleSignEnum.class, record.getRoleSign());
                record.setRoleSignName(signEnum == null ? "" : signEnum.getShowName());

                RoleTypeEnum typeEnum = Enums.of(RoleTypeEnum.class, record.getRoleType());
                record.setRoleTypeName(typeEnum == null ? "" : typeEnum.getShowName());

                RoleDataAuthEnum dataAuthEnum = Enums.of(RoleDataAuthEnum.class, record.getRoleDataAuth());
                record.setRoleDataAuthName(dataAuthEnum == null ? "" : dataAuthEnum.getShowName());
            }
            return ResultUtil.layData(0, "", data.getTotal(), records);
        }
        return ResultUtil.layData(0, "", 0, null);
    }
}
